import { Models } from '../../../../common/typings/model'
import Config from '../../../../config/Config'
import KoaRouter from 'koa-router'
import Mysql from '../../../../core/Mysql'
import { Success } from '../../../../core/HttpException'
import validator from '../../../../middlewares/validator'
import Auth from '../../../../middlewares/Auth'
import editRolePermissionById from '../../../../common/apiValidate/system/role/editPermission'

const router = new KoaRouter({
  prefix: `${Config.api_prefix}system/role`,
})

router.post(
  '/editPermission',
  Auth.verifyTokenPermission,
  validator(editRolePermissionById, 'body'),
  async (ctx: Models.Ctx) => {
    const { ids, roleId } = ctx.request.body
    await Mysql.command(`
    UPDATE
      role
    SET permissions = '${ids}'
    WHERE id = ${roleId}
  `)

    throw new Success()
  }
)

export default router
